// @ts-nocheck

import {serializeAsJSON,} from "../packages/excalidraw/data/json";
import {StoreAction} from "../packages/excalidraw/store";
import type {ActionResult} from "../packages/excalidraw/actions/types";
import {exportCanvas, prepareElementsForExport} from "../packages/excalidraw/data";

export const FocusAnyEditor = {
  run: (func: string, success: any, error: any, data: any, silence: boolean) => {
    try {
      if (!silence) {
        console.log('FocusAnyEditor.run', func, {success, error, data});
      }
      if (!FocusAnyEditor[func]) {
        setTimeout(() => {
          FocusAnyEditor.run(func, success, error, data);
        }, 100)
        return;
      }
      FocusAnyEditor[func](success, error, data);
    } catch (e) {
      error && error(`RunError: ${e}`);
    }
  },
  fireChangeWithoutData: () => {
    if (!FocusAnyEditor.getContent) {
      return;
    }
    FocusAnyEditor.getContent((data: any) => {
      FocusAnyEditor.fireChange(data);
    })
  },
  fireChange: (data: any) => {
    window.parent.postMessage({
      type: 'ContentChange',
      data: data
    }, '*');
  },
  editorInit: null as any,
  getContent: null as any,
  setContent: null as any,
  export: null as any,
}

window.editorInit = (success, error) => {
  FocusAnyEditor.run('editorInit', success, error);
};
window.editorGetContent = (success, error) => {
  FocusAnyEditor.run('getContent', success, error);
};
window.editorSetContent = (success, error, content) => {
  FocusAnyEditor.run('setContent', success, error, content);
};
window.editorExport = (success, error, type) => {
  FocusAnyEditor.run('export', success, error, type);
}

export const useFocusAnyEditor = (data: {
  manager: any,
}) => {
  FocusAnyEditor.editorInit = (success: any, error: any) => {
    success && success();
  };
  FocusAnyEditor.setContent = (success: any, error: any, content: any) => {
    const {
      elements,
      appState,
      files,
    } = JSON.parse(content);
    data.manager.updater({
      elements: elements || [],
      appState: appState || {},
      files: files || {},
      storeAction: StoreAction.CAPTURE
    } as ActionResult);
    success && success();
  };
  FocusAnyEditor.getContent = (success: any, error: any) => {
    const serialized = serializeAsJSON(
      data.manager.getElementsIncludingDeleted(),
      data.manager.getAppState(),
      data.manager.app.files,
      "local"
    );
    success && success(serialized)
  };
  FocusAnyEditor.export = (success: any, error: any, type: any) => {
    const {
      exportingFrame,
      exportedElements
    } = prepareElementsForExport(
      data.manager.app.visibleElements,
      data.manager.app.state,
      false
    )
    exportCanvas(
      type,
      exportedElements,
      data.manager.app.state,
      data.manager.app.files,
      {
        exportBackground: data.manager.app.state.exportBackground,
        name: 'Image',
        viewBackgroundColor: data.manager.app.state.viewBackgroundColor,
        exportingFrame: exportingFrame,
      },
      success
    )
  }
}

